﻿
// catheutils.cs:缓存的工具类
// CopyRight (c) 2009-2010 HangJing Teconology. All Rights Reserved.
// zjf@ihangjing.com
// 2010-03-05

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Caching;
using System.Text.RegularExpressions;

//缓存
//this codes copy from web
//add by zhengjianfeng 2010－01－12
using Hangjing.Common;

public class CacheUtils
{
    // 字段
    private static readonly Cache _cache;
    public static readonly int DayFactor = 0x4380;
    private static int Factor = 5;
    public static readonly int HourFactor = 720;
    public static readonly int MinuteFactor = 12;
    public static readonly double SecondFactor = 0.2;

    // 方法
    static CacheUtils()
    {
        HttpContext current = HttpContext.Current;
        if (current != null)
        {
            _cache = current.Cache;
        }
        else
        {
            _cache = HttpRuntime.Cache;
        }
    }

    private CacheUtils()
    {
    }

    public static void Clear()
    {
        IDictionaryEnumerator enumerator = _cache.GetEnumerator();
        ArrayList list = new ArrayList();
        while (enumerator.MoveNext())
        {
            list.Add(enumerator.Key);
        }
        foreach (string str in list)
        {
            _cache.Remove(str);
        }
    }

    public static object Get(string key)
    {
        return _cache[key];
    }

    public static string GetCacheKeyByUserName(string key, string userName)
    {
        return string.Format("{0}_BY_USER_{1}", key, userName);
    }

    public static void Insert(string key, object obj)
    {
        Insert(key, obj, null, 1);
    }

    public static void Insert(string key, object obj, int seconds)
    {
        Insert(key, obj, null, seconds);
    }

    public static void Insert(string key, object obj, CacheDependency dep)
    {
        Insert(key, obj, dep, HourFactor * 12);
    }

    public static void Insert(string key, object obj, int seconds, CacheItemPriority priority)
    {
        Insert(key, obj, null, seconds, priority);
    }

    public static void Insert(string key, object obj, CacheDependency dep, int seconds)
    {
        Insert(key, obj, dep, seconds, CacheItemPriority.Normal);
    }

    public static void Insert(string key, object obj, CacheDependency dep, int seconds, CacheItemPriority priority)
    {
        if (obj != null)
        {
            _cache.Insert(key, obj, dep, DateTime.Now.AddSeconds((double)(Factor * seconds)), TimeSpan.Zero, priority, null);
        }
    }

    public static void Max(string key, object obj)
    {
        Max(key, obj, null);
    }

    public static void Max(string key, object obj, CacheDependency dep)
    {
        if (obj != null)
        {
            _cache.Insert(key, obj, dep, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.AboveNormal, null);
        }
    }

    public static void MicroInsert(string key, object obj, int secondFactor)
    {
        if (obj != null)
        {
            _cache.Insert(key, obj, null, DateTime.Now.AddSeconds((double)(Factor * secondFactor)), TimeSpan.Zero);
        }
    }

    public static void Remove(string key)
    {
        _cache.Remove(key);
    }

    public static void RemoveByPattern(string pattern)
    {
        IDictionaryEnumerator enumerator = _cache.GetEnumerator();
        Regex regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
        while (enumerator.MoveNext())
        {
            if (regex.IsMatch(enumerator.Key.ToString()))
            {
                _cache.Remove(enumerator.Key.ToString());
            }
        }
    }

    public static void RemoveByStartString(string startString)
    {

        if (!StringHelper.IsNullorEmpty(startString))
        {
            RemoveByPattern(startString + "([w+]*)");
        }
    }

    public static void ReSetFactor(int cacheFactor)
    {
        Factor = cacheFactor;
    }

    public static int SecondFactorCalculate(int seconds)
    {
        return Convert.ToInt32(Math.Round((double)(seconds * SecondFactor)));
    }
}



